﻿using System;
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
using System.Windows;
using System.Collections.Generic;
using Enough.Converter;
using System.Linq;

namespace WP.UnitTests
{
    [TestClass]
    public class ConverterTests
    {
        [TestMethod]
        public void TestCollectionSizeConverters()
        {
            CollectionSizeToVisibilityConverter converter = new CollectionSizeToVisibilityConverter();
            List<string> values = new List<string>();
            Visibility visibility = (Visibility)converter.Convert(values, null, null, null);
            Assert.AreEqual(Visibility.Visible, visibility);
            visibility = (Visibility)converter.Convert(values, null, "reverse", null);
            Assert.AreEqual(Visibility.Collapsed, visibility);
            visibility = (Visibility)converter.Convert(values, null, "0", null);
            Assert.AreEqual(Visibility.Visible, visibility);
            visibility = (Visibility)converter.Convert(values, null, "1", null);
            Assert.AreEqual(Visibility.Collapsed, visibility);
            IEnumerable<string> orderedValues = values.OrderBy(v => v);
            visibility = (Visibility)converter.Convert(orderedValues, null, null, null);
            Assert.AreEqual(Visibility.Visible, visibility);
            visibility = (Visibility)converter.Convert(orderedValues, null, "reverse", null);
            Assert.AreEqual(Visibility.Collapsed, visibility);

            values.Add("hello");

            visibility = (Visibility)converter.Convert(values, null, null, null);
            Assert.AreEqual(Visibility.Collapsed, visibility);
            visibility = (Visibility)converter.Convert(values, null, "0", null);
            Assert.AreEqual(Visibility.Collapsed, visibility);
            visibility = (Visibility)converter.Convert(values, null, "1", null);
            Assert.AreEqual(Visibility.Visible, visibility);
            visibility = (Visibility)converter.Convert(values, null, "reverse", null);
            Assert.AreEqual(Visibility.Visible, visibility);

            orderedValues = values.OrderBy(v => v);
            visibility = (Visibility)converter.Convert(orderedValues, null, null, null);
            Assert.AreEqual(Visibility.Collapsed, visibility);
            visibility = (Visibility)converter.Convert(orderedValues, null, "reverse", null);
            Assert.AreEqual(Visibility.Visible, visibility);
            visibility = (Visibility)converter.Convert(orderedValues, null, "0", null);
            Assert.AreEqual(Visibility.Collapsed, visibility);
            visibility = (Visibility)converter.Convert(orderedValues, null, "1", null);
            Assert.AreEqual(Visibility.Visible, visibility);

            values.Add("world");
            values.Add("again");

            visibility = (Visibility)converter.Convert(values, null, null, null);
            Assert.AreEqual(Visibility.Collapsed, visibility);
            visibility = (Visibility)converter.Convert(values, null, "reverse", null);
            Assert.AreEqual(Visibility.Visible, visibility);
            visibility = (Visibility)converter.Convert(values, null, "3", null);
            Assert.AreEqual(Visibility.Visible, visibility);

            orderedValues = values.OrderBy(v => v);
            visibility = (Visibility)converter.Convert(orderedValues, null, null, null);
            Assert.AreEqual(Visibility.Collapsed, visibility);
            visibility = (Visibility)converter.Convert(orderedValues, null, "reverse", null);
            Assert.AreEqual(Visibility.Visible, visibility);
            visibility = (Visibility)converter.Convert(orderedValues, null, "3", null);
            Assert.AreEqual(Visibility.Visible, visibility);

            orderedValues = values.Where(v => v == "not there");
            Assert.AreEqual(0, orderedValues.Count());
            visibility = (Visibility)converter.Convert(orderedValues, null, null, null);
            Assert.AreEqual(Visibility.Visible, visibility);
            visibility = (Visibility)converter.Convert(orderedValues, null, "reverse", null);
            Assert.AreEqual(Visibility.Collapsed, visibility);
            visibility = (Visibility)converter.Convert(orderedValues, null, "0", null);
            Assert.AreEqual(Visibility.Visible, visibility);

            orderedValues = values.Where(v => v == "again");
            Assert.AreEqual(1, orderedValues.Count());
            visibility = (Visibility)converter.Convert(orderedValues, null, "0", null);
            Assert.AreEqual(Visibility.Collapsed, visibility);
            visibility = (Visibility)converter.Convert(orderedValues, null, "1", null);
            Assert.AreEqual(Visibility.Visible, visibility);
            visibility = (Visibility)converter.Convert(orderedValues, null, "2", null);
            Assert.AreEqual(Visibility.Collapsed, visibility);

        }
    }
}
